<template>
  <div class="search-orders-container">
    <el-card class="table-card" :body-style="{ padding: '0' }">
      <template #header>
        <div class="header">
          <h2>预约订单列表</h2>
          <div class="header-right">
            <el-tag type="info" effect="dark" class="header-tag">总订单: {{ orders.length }}</el-tag>
          </div>
        </div>
      </template>

      <div class="filter-container">
        <el-form :model="filterForm" label-width="80px" class="search-form">
          <div class="form-row">
            <el-form-item label="会议室" class="form-item">
              <el-select v-model="filterForm.roomId" placeholder="请选择会议室" filterable class="custom-select">
                <el-option
                  v-for="room in rooms"
                  :key="room.id"
                  :label="room.name"
                  :value="room.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="开始日期" class="form-item">
              <el-date-picker 
                v-model="filterForm.startDate" 
                type="date" 
                placeholder="选择开始日期" 
                class="custom-date-picker"
              />
            </el-form-item>
            <el-form-item label="结束日期" class="form-item">
              <el-date-picker 
                v-model="filterForm.endDate" 
                type="date" 
                placeholder="选择结束日期" 
                class="custom-date-picker"
              />
            </el-form-item>
            <el-form-item label="状态" class="form-item">
              <el-select v-model="filterForm.status" placeholder="请选择状态" class="custom-select">
                <el-option label="全部" value="" />
                <el-option label="审核" value="pending" />
                <el-option label="确认" value="confirmed" />
                <el-option label="未支付" value="not_paid" />
                <el-option label="取消" value="cancelled" />
                <el-option label="退款" value="refunded" />
              </el-select>
            </el-form-item>
          </div>
          <div class="form-actions">
            <el-button type="primary" @click="handleSearch" class="search-button">
              <i class="el-icon-search"></i> 搜索
            </el-button>
            <el-button @click="resetFilter" class="reset-button">
              <i class="el-icon-refresh"></i> 重置
            </el-button>
          </div>
        </el-form>
      </div>

      <div class="table-container">
        <el-table 
          :data="orders" 
          style="width: 100%" 
          v-loading="loading"
          element-loading-text="加载中..."
          element-loading-background="rgba(255, 255, 255, 0.8)"
          :header-cell-style="{background:'#f8faff', color: '#1a237e'}"
          :row-class-name="tableRowClassName"
        >
          <el-table-column prop="roomName" label="会议室" min-width="120">
            <template #default="scope">
              <div class="cell-with-icon">
                <i class="el-icon-office-building cell-icon"></i>
                <span>{{ scope.row.roomName }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="bookingDate" label="日期" min-width="120">
            <template #default="scope">
              <div class="cell-with-icon">
                <i class="el-icon-date cell-icon"></i>
                <span>{{ scope.row.bookingDate }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="startHour" label="开始时间" min-width="120">
            <template #default="scope">
              <div class="cell-with-icon">
                <i class="el-icon-time cell-icon"></i>
                <span>{{ formatTime(scope.row.startHour) }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="endHour" label="结束时间" min-width="120">
            <template #default="scope">
              <div class="cell-with-icon">
                <i class="el-icon-time cell-icon"></i>
                <span>{{ formatTime(scope.row.endHour) }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" min-width="100">
            <template #default="scope">
              <el-tag 
                :type="getStatusType(scope.row.status)" 
                effect="light"
                class="status-tag"
              >
                {{ getStatusText(scope.row.status) }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useAppointmentStore } from '../stores/AppointmentStore'
import { ElMessage } from 'element-plus'
import { useRoomStore } from '../stores/RoomStore' 

const appointmentStore = useAppointmentStore()
const roomStore = useRoomStore()
const loading = ref(false)
const orders = ref([])
const rooms = ref([])

// 表格行样式
const tableRowClassName = ({ row }) => {
  if (row.status === 'pending') return 'pending-row'
  if (row.status === 'not_paid') return 'not-paid-row'
  if (row.status === 'refunded') return 'refunded-row'
  return ''
}

const filterForm = ref({
  roomId: '',
  startDate: '',
  endDate: '',
  status: ''
})

// 格式化时间
const formatTime = (time) => {
  if (!time) return '未设置'
  return time.slice(0, 5)
}

// 获取状态类型
const getStatusType = (status) => {
  switch (status) {
    case 'confirmed': return 'success'
    case 'pending': return 'warning'
    case 'cancelled': return 'info'
    case 'not_paid': return 'warning'
    case 'refunded': return 'danger'
    default: return 'info'
  }
}

// 获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 'confirmed': return '确认'
    case 'pending': return '审核'
    case 'cancelled': return '取消'
    case 'not_paid': return '未支付'
    case 'refunded': return '退款'
    default: return status
  }
}

// 搜索订单
const handleSearch = async () => {
  loading.value = true
  try {
    const response = await appointmentStore.fetchAllAppointments(filterForm.value)
    if (response.code === 200) {
      orders.value = response.data
    } else {
      ElMessage.error(response.message || '获取订单失败')
    }
  } catch (error) {
    console.error('获取订单失败:', error)
    ElMessage.error('获取订单失败')
  } finally {
    loading.value = false
  }
}

// 重置筛选条件
const resetFilter = () => {
  filterForm.value = {
    roomId: '',
    startDate: '',
    endDate: '',
    status: ''
  }
  handleSearch()
}

// 组件挂载时获取数据
onMounted(async () => {
  await fetchRooms()
  handleSearch()
})

// 获取所有会议室
const fetchRooms = async () => {

  const response = await roomStore.getAllRoomsName()
  rooms.value = response

}
</script>

<style scoped>
.search-orders-container {
  min-height: 100vh;
  margin-left: 12px;
  background-color: #f5f7fa;
  background-image: linear-gradient(135deg, #f5f7fa 0%, #eef2f7 100%);
}

.header {
  padding: 24px;
  border-radius: 0 0 16px 16px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
  margin-bottom: 32px;
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
}

.header h2 {
  margin: 0;
  color: rgb(37, 37, 133);
  font-size: 28px;
  font-weight: 600;
  background: rgb(37, 37, 133);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-tag {
  font-size: 16px;
  font-weight: 500;
  border-radius: 12px;
  padding: 6px 18px;
  background: rgba(34, 34, 116, 0.15);
  color: #18195a;
  box-shadow: 0 2px 8px rgba(57, 73, 171, 0.08);
}

/* 卡片样式增强 */
.table-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: all 0.3s ease;
  border: none;
  margin-bottom: 24px;
}

.table-card:hover {
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.12);
  transform: translateY(-2px);
}

.card-header {
  position: sticky;
  top: 0;
  padding: 20px 24px;
  border-bottom: 1px solid #eef2f7;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #3949ab 0%, #5c6bc0 100%);
  z-index: 20;
  transition: all 0.3s ease;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.13);
  border-radius: 0 0 16px 16px;
}

.filter-container {
  position: sticky;
  top: 72px;
  z-index: 15;
  padding: 20px 24px 12px 24px;
  border-bottom: 1px solid #eef2f7;
  background: rgba(252, 253, 255, 0.98);
  box-shadow: 0 2px 8px rgba(57, 73, 171, 0.06);
  border-radius: 0 0 12px 12px;
  margin-bottom: 12px;
}

.search-orders-container {
  padding: 0px 10px 32px 24px;
  min-height: 100vh;
  margin-left: 12px;
  background-color: #f5f7fa;
  background-image: linear-gradient(135deg, #f5f7fa 0%, #eef2f7 100%);

}

.table-container {
  padding: 0;
  overflow-x: auto;
}

/* 筛选表单样式 */
.filter-container {
  padding: 20px 24px;
  border-bottom: 1px solid #eef2f7;
  background-color: #fcfdff;
}

.search-form {
  width: 100%;
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.form-item {
  margin-bottom: 0;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

:deep(.el-form-item__label) {
  color: #1a237e;
  font-weight: 500;
}

:deep(.el-input__inner) {
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  padding: 12px 16px;
  transition: all 0.3s ease;
}

:deep(.el-input__inner:focus) {
  border-color: #3949ab;
  box-shadow: 0 0 0 2px rgba(57, 73, 171, 0.1);
}

.custom-select {
  width: 100%;
}

.custom-date-picker {
  width: 100%;
}

:deep(.el-date-editor .el-input__inner) {
  padding-left: 40px;
}

/* 按钮样式 */
.search-button,
.reset-button {
  padding: 10px 20px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.search-button {
  background: linear-gradient(90deg, #3949ab 0%, #5c6bc0 100%);
  border: none;
  color: white;
}

.search-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(57, 73, 171, 0.3);
}

.reset-button {
  border: 1px solid #e0e0e0;
  color: #546e7a;
}

.reset-button:hover {
  border-color: #3949ab;
  color: #3949ab;
  background: rgba(57, 73, 171, 0.05);
}

/* 表格容器 */
.table-container {
  padding: 0;
  overflow-x: auto;
}

/* 表格样式增强 */
:deep(.el-table) {
  border-radius: 0;
  overflow: hidden;
  --el-table-border-color: #eef2f7;
  --el-table-header-bg-color: #f8faff;
}

:deep(.el-table::before) {
  display: none;
}

:deep(.el-table th) {
  background-color: #f8faff !important;
  color: #1a237e;
  font-weight: 600;
  padding: 16px;
  border-bottom: 2px solid #eef2f7;
}

:deep(.el-table td) {
  color: #546e7a;
  padding: 16px;
  border-bottom: 1px solid #eef2f7;
}

:deep(.el-table .pending-row) {
  background-color: rgba(255, 248, 227, 0.3);
}

:deep(.el-table .not-paid-row) {
  background-color: rgba(255, 236, 236, 0.3);
}

:deep(.el-table .refunded-row) {
  background-color: rgba(232, 245, 233, 0.3);
}

:deep(.el-table .pending-row:hover > td),
:deep(.el-table .not-paid-row:hover > td),
:deep(.el-table .refunded-row:hover > td) {
  background-color: rgba(248, 250, 255, 0.5) !important;
}

:deep(.el-table .el-table__row:hover > td) {
  background-color: #f8faff;
}

/* 单元格内容样式 */
.cell-with-icon {
  display: flex;
  align-items: center;
}

.cell-icon {
  color: #3949ab;
  margin-right: 8px;
  font-size: 16px;
}

/* 状态标签样式 */
.status-tag {
  border-radius: 20px;
  padding: 4px 12px;
  font-weight: 500;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 80px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* 响应式调整 */
@media (max-width: 768px) {
  .search-orders-container {
    padding: 16px;
  }

  .card-header {
    padding: 16px;
    flex-direction: column;
    align-items: flex-start;
  }

  .header-right {
    margin-top: 8px;
  }

  .filter-container {
    padding: 16px;
  }

  .form-row {
    grid-template-columns: 1fr;
    gap: 16px;
    margin-bottom: 16px;
  }

  .form-actions {
    flex-direction: column;
    gap: 8px;
  }

  :deep(.el-table th),
  :deep(.el-table td) {
    padding: 12px;
  }
}
</style>